#pragma comment(linker, "/STACK:10000000")
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <set>
#include <map>
#include <cstdio>
#include <cassert>
#include <string>
#include <cstring>
#define ldb long double
#define LL long long
#define fi first
#define se second
#define fill(a, c) memset(a, c, sizeof(a))
#define sqr(a) ((a) * (a))
#define nextLine() {int c = 0; while((c = getchar()) != 10 && c != EOF);}
#define getBit(mask, k) (((mask) / pw[k]) % pw[1])
#define setBit(mask, k, l) (((mask) / pw[k + 1] * pw[1] + (l)) * pw[k] + ((mask) % pw[k]))
#define debug(a) cerr << #a << " = " << a << " ";
#define debugl(a) cerr << #a << " = " << a << "\n";
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define ff first
#define ss second 
const ldb LDINF = 9128739847123.00;
const ldb eps = 1e-9;
const int inf = 1 << 28;
const ldb pi = fabsl(atan2l(0.0, -1.0));
using namespace std;


int rev(int mask, int p)
{
	int _mask = 0;
	for (int j = 0; j < p; j++)
	{
		_mask <<= 1;
		if (mask & (1 << j))
			_mask |= 1;		
	}
	return _mask;
}

LL n;
LL M;
int m;

#define mod(a, n) ((a) % (n))

LL RoundUp(LL a, LL b)
{
	LL t = a / b;
	if (a - t * b > 0) t++;
	return t;
}

LL RoundDown(LL a, LL b)
{
	LL t = a / b;
	if (a - t * b < 0) t--;
	return t;
}


LL get(LL L, LL R, LL from, LL step, LL l, LL r, LL u, LL v)
{
//	cerr << L << " " << R << " " << from << " " << step << " " << l << " " << r << " " << u << " " << v << "\n";
	if (l > R || L > r) return 0;
	if (R - L + 1 < 0) return 0;
	if (l <= L && r >= R)
	{
		LL f1 = RoundUp(u - from, step);
		LL f2 = RoundDown(v - from, step);
		if (f1 < 0) f1 = 0;
		if (f1 > R - L) return 0;
		if (f2 > R - L) f2 = R - L;
		if (f2 < 0) return 0;
		if (f1 > f2) return 0;
		LL r1 = 2ll * (from % M) + ((f1 + f2)) * (step % M);
		LL r2 = f2 - f1 + 1;
		if ((f1 + f2) % 2ll == 0) r1 >>= 1;
		else r2 >>= 1;
		r1 %= M;
		r2 %= M;
		r1 *= r2;
		r1 %= M;
		return r1;
	}
	if ((R - L + 1) % 2 == 0)
	{
		return mod(get(L, L + (R - L + 1) / 2 - 1, from, step * 2, l, r, u, v)
		+ get(L + (R - L + 1) / 2, R, from + step, step * 2, l, r, u, v), M);
	}
	else
	{
		return mod(get(L, L + (R - L + 1) / 2, from, step * 2, l, r, u, v)
		+ get(L + (R - L + 1) / 2 + 1, R, from + step, step * 2, l, r, u, v), M);
	}
}

void load()
{
	cin >> n >> m >> M;
/*	int p = 1;
	 while ((1 << p) < n) p++;
	 for (int i = 0; i < n; i++)
	 {
	 	cout << rev(i, p) << " ";
	 }
	 */
	LL l, r, u, v;
	for (int i = 0; i < m; i++)
	{
		scanf("%I64d%I64d%I64d%I64d", &l, &r, & u, & v);
		printf("%I64d\n", get(1, n, 1, 1, l, r, u, v));
	}
}

void solve()
{


}

#define file "d"
int main()
{
	#ifndef ONLINE_JUDGE
		freopen(file".in", "rt", stdin);
		freopen(file".out", "wt", stdout);
	#endif
	load();
	solve();
	return 0;
}
